﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.leave;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.leave
{
    public class GetDelparmentmanagerHandler : IRequestHandler<GetDelparmentmanagerCommon, ApiResult<string>>
    {
        private readonly IBaseRepository<DepartmentModel> deparmentRepo;
        private readonly IBaseRepository<UserModel> userRepo;

        public GetDelparmentmanagerHandler(IBaseRepository<DepartmentModel> deparmentRepo,IBaseRepository<UserModel> userRepo)
        {
            this.deparmentRepo = deparmentRepo;
            this.userRepo = userRepo;
        }
        public Task<ApiResult<string>> Handle(GetDelparmentmanagerCommon request, CancellationToken cancellationToken)
        {
            ApiResult<string> res = new ApiResult<string>();

            var info = deparmentRepo.GetAll().FirstOrDefault(x => x.DepartmentId == request.DepartmentId).ParentId;

            var user= userRepo.GetAll().FirstOrDefault(x => x.UserId == info).UserName;

            res.Code = ApiEnum.Success;
            res.Message = "部门领导查询成功";
            res.Data=user.ToString();
            return Task.FromResult(res);


        }
    }
}
